package com.cn.parks.servlet;

import com.cn.parks.dao.UserDao;
import com.cn.parks.dao.impl.UserDaoImpl;
import com.cn.parks.entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/**
 * 用户管理Servlet
 */
@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
    private UserDao userDao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        response.setContentType("application/json;charset=UTF-8");
        
        try {
            if ("/list".equals(pathInfo)) {
                // 获取所有用户
                List<User> users = userDao.getAll();
                // 构造JSON响应
                StringBuilder json = new StringBuilder();
                json.append("{\"success\":true,\"data\":[");
                
                for (int i = 0; i < users.size(); i++) {
                    User user = users.get(i);
                    if (i > 0) {
                        json.append(",");
                    }
                    json.append("{")
                        .append("\"userId\":\"").append(user.getUserId()).append("\",")
                        .append("\"userName\":\"").append(user.getUserName()).append("\",")
                        .append("\"realName\":\"").append(user.getRealName()).append("\",")
                        .append("\"roleId\":\"").append(user.getRoleId()).append("\",")
                        .append("\"userPhone\":\"").append(user.getUserPhone()).append("\"")
                        .append("}");
                }
                
                json.append("]}");
                response.getWriter().write(json.toString());
            } else {
                response.getWriter().write("{\"success\":false,\"message\":\"Invalid path\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("{\"success\":false,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        response.setContentType("application/json;charset=UTF-8");
        
        try {
            if ("/add".equals(pathInfo)) {
                // 添加用户
                User user = new User();
                user.setUserName(request.getParameter("userName"));
                user.setRealName(request.getParameter("realName"));
                user.setUserPwd(request.getParameter("password"));
                user.setRoleId(request.getParameter("roleId"));
                user.setUserPhone(request.getParameter("userPhone"));
                
                userDao.insert(user);
                response.getWriter().write("{\"success\":true,\"message\":\"添加成功\"}");
            } else if ("/update".equals(pathInfo)) {
                // 更新用户
                User user = new User();
                user.setUserName(request.getParameter("userName"));
                user.setRealName(request.getParameter("realName"));
                user.setRoleId(request.getParameter("roleId"));
                user.setUserPhone(request.getParameter("userPhone"));
                
                userDao.update(user);
                response.getWriter().write("{\"success\":true,\"message\":\"更新成功\"}");
            } else if ("/delete".equals(pathInfo)) {
                // 删除用户
                String userId = request.getParameter("userId");
                userDao.deleteById(userId);
                response.getWriter().write("{\"success\":true,\"message\":\"删除成功\"}");
            } else {
                response.getWriter().write("{\"success\":false,\"message\":\"Invalid path\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("{\"success\":false,\"message\":\"" + e.getMessage() + "\"}");
        }
    }
} 